package com.hibernate.rcache.region;

import com.rcache.core.cache.Rcache;
import com.rcache.core.manager.CacheManager;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.internal.Timestamper;
import org.hibernate.cache.spi.CacheDataDescription;
import org.hibernate.cache.spi.NaturalIdRegion;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy;
import org.hibernate.cfg.Settings;

import java.util.Map;
import java.util.Properties;

public class RcacheNaturalIdRegion implements NaturalIdRegion {
    private final CacheManager cacheManager;
    private final Rcache cache;
    private final CacheDataDescription metadata;
    private final Settings settings;
    private final Properties properties;

    public RcacheNaturalIdRegion(CacheManager cacheManager, Rcache cache, Properties properties, Settings settings, CacheDataDescription metadata) {
        this.cacheManager=cacheManager;
        this.cache=cache;
        this.metadata=metadata;
        this.settings=settings;
        this.properties=properties;
    }

    public NaturalIdRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException {
        return null;
    }

    public boolean isTransactionAware() {
        return false;
    }

    public CacheDataDescription getCacheDataDescription() {
        return metadata;
    }

    public String getName() {
        return cache.getCacheName();
    }

    public void destroy() throws CacheException {
        cacheManager.remove(cache.getCacheName());
    }

    public boolean contains(Object key) {
        return cache.containsKey(key);
    }

    public long getSizeInMemory() {
        return cacheManager.getSizeInMemory();
    }

    public long getElementCountInMemory() {
        return cacheManager.getElementCountInMemory();
    }

    public long getElementCountOnDisk() {
        return cache.getDiskCacheSize();
    }

    public Map toMap() {
        return cache.toMap();
    }

    public long nextTimestamp() {
        return Timestamper.next();
    }

    public int getTimeout() {
        return cacheManager.getTimeout();
    }
}
